/**
 * Created by zhanglihe on 16/1/23.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog'
], function ($, common, ready, Collection, template, Model, dialog) {

    var id = getUrlPath(3);

    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetchTagName();
            this.fetch();
            this.event();
        },
        fetchTagName: function () {
            $.ajax({
                url: _global.url.api + 'newone_group/' + id,
                type: 'get',
                success: function (res) {
                    if (res.code == 0) {
                        $('#tag_name').html(res.data.group_name);
                    }
                }
            });
        },
        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'newone_group/goods_list/' + id,
                model: {
                    url: {
                        delete: _global.url.api + 'newone_group_relation/goods/{id}'
                    },
                    event: {
                        '.item-remove': 'remove'
                    },
                    remove: function (e) {
                        e.preventDefault();
                        this.trash(id, this.get_data('goods_id'));
                    },
                    trash: function (group_id, goods_id) {
                        $.ajax({
                            url: _global.url.api + 'newone_group_relation/goods',
                            type: 'delete',
                            async: false,
                            data: {group_id: group_id, goods_id: goods_id},
                            success: function (res) {
                                if (res.code == 0) {
                                    _this.collection.reload();
                                } else {
                                    Alert({msg: res.msg, type: 'error'});
                                }
                            }, error: function () {
                                Alert({msg: '通讯错误', type: 'error'});
                            }
                        });
                    }
                },
                callback: function () {
                    loaded();//移除加载效果
                }
            });
            this.collection.fetch();
        },
        imgPreview: function (img, item_id) {
            if (typeof img.id !== 'undefined') {
                img.item_id = item_id;
                $('#' + item_id).html(template('img-tpl', img));
            }
        },
        event: function () {
            var _this = this;

            //上传图片
            $('body').on('click', '.btn-upload', function (e) {
                var item_id = $(this).attr('data-id');
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function () {
                        var img = $("#upload-frame")[0].contentWindow.image.get();
                        _this.imgPreview(img, item_id);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog.show();
            });
            $('#btn-add').on('click', function (e) {
                e.preventDefault();
                dialog({
                    title: '增加商品',
                    width: '500',
                    content: template('add-tpl', {type: id}),
                    ok: function () {
                        var data = {
                            goods_id: $('[name="goods_id"]').val(),
                            new_user_image: $('[name="new_user_image"]').val(),
                            old_user_image: $('[name="old_user_image"]').val(),
                            group_id: id
                        };
                        new Model({
                            url: _global.url.api + 'newone_group_relation/goods',
                            data: data,
                            success: function () {
                                _this.collection.reload();
                            }
                        }).post();
                    }, cancel: function () {
                    }
                }).show();
            });
        }
    };
    new index();
});